<!-- eslint-disable no-unreachable -->
<template>
    <div>
  
      <el-container>
        <!-- 搜索栏 -->
        <el-header>
          <el-row :gutter="20" style="padding: 5px 20px;">
            <el-col :span="6"><el-input v-model="searchModel.deviceName" placeholder="设备名称" clearable @keyup.enter.native="getModelSensorList" /></el-col>
            <el-col :span="6"><el-button type="primary" round icon="el-icon-search" @click="getModelSensorList">搜索</el-button></el-col>
            <el-col align="right" :span="6"><el-button type="primary" round icon="el-icon-plus" @click="handleDialogVisible(null)">新增</el-button></el-col>
          </el-row>
        </el-header>
  
        <!-- 结果列表 -->
        <el-main>
          <el-table :data="list" style="width: 100%" :header-cell-style="centerHeaderStyle" :cell-style="centerCellStyle">
            <el-table-column type="index" label="index" width="100">
              <template slot-scope="scope">
                {{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column prop="deviceName" label="设备模型名称"/>
            <el-table-column prop="deviceType" label="设备模型型号"/>
            <el-table-column prop="createTime" label="创建时间" />
            <el-table-column prop="upInterval" label="上报间隔" />
            <el-table-column prop="protocol" label="协议" />
            <el-table-column prop="id" label="数据点">
            <template slot-scope="scope">
              <el-popover
              placement="right"
              width="400"
              trigger="click">
              <el-form label-width="80px" :model="modelForm" label-position="left">
                <el-form-item label="名称">
                  <el-input v-model="modelForm.dataPointName" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                  <el-input v-model="modelForm.dataPointExtra" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="阙值A">
                  <el-input v-model="modelForm.lowThreshold" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="阙值B">
                  <el-input v-model="modelForm.highThreshold" :disabled="true"></el-input>
                </el-form-item>
              </el-form>
              <el-button slot="reference" @click="getModelForm(scope.row.id)">查看组织</el-button>
            </el-popover>
            </template>
          </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  icon="el-icon-edit"
                  @click="handleDialogVisible(scope.row.id)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="deleteModelSensor(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
  
        <!-- 分页 -->
        <el-footer>
          <el-pagination
            :current-page="searchModel.pageNo"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="searchModel.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalPage"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-footer>
      </el-container>
  
      <!-- 修改框 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearForm">
        <el-form ref="formRef" :model="form" :rules="rules">
          <el-form-item prop="deviceName" label="设备模型名称" :label-width="formLabelWidth">
            <el-input v-model="form.deviceName" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="deviceType" label="设备模型型号" :label-width="formLabelWidth">
            <el-select v-model="form.deviceType" placeholder="请选择" style="width: 100%;">
              <el-option
                v-for="item in sensorForm"
                :key="item.sensorName"
                :label="item.sensorName"
                :value="item.sensorName">
              </el-option>
            </el-select>
          </el-form-item>
         
          <el-form-item prop="protocol" label="协议选择" :label-width="formLabelWidth">
            <el-radio-group v-model="form.protocol">
              <el-radio label="MQTT">MQTT</el-radio>
              <el-radio label="COAP">COAP</el-radio>
              <el-radio label="NB-loT">NB-loT</el-radio>
              <el-radio label="LWM2M">LWM2M</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="upInterval" label="上报间隔(秒)" :label-width="formLabelWidth">
            <el-input v-model="form.upInterval" autocomplete="off" />
          </el-form-item>
          <el-form-item label="数据点" prop="" :label-width="formLabelWidth">
          </el-form-item>
          <el-form-item prop="dataPointName" label="名称" :label-width="formLabelWidth">
            <el-input v-model="form.dataPointName" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="lowThreshold" label="阙值A" :label-width="formLabelWidth">
            <el-input v-model="form.lowThreshold" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="highThreshold" label="阙值B" :label-width="formLabelWidth">
            <el-input v-model="form.highThreshold" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="dataPointExtra" label="备注" :label-width="formLabelWidth">
            <el-input v-model="form.dataPointExtra" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import modelSensorRequest from '@/api/modelSensorManage'
  import sensorRequest from '@/api/sensorManage'
  export default {
    data() {
      return {
        sensorForm: {},
        modelForm: {},
        totalPage: 0,
        searchModel: {
          deviceName: '',
          pageNo: 1,
          pageSize: 10
        },
        list: [],
        dialogFormVisible: false, // 对话框显示隐藏
        form: {
          
        },
        formLabelWidth: '150px',
        title: '',
        rules: {
          deviceName: [
            { required: true, message: '请输入角色名', trigger: 'blur' },
            { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getModelSensorList()
      this.getSensorForm()
    },
    methods: {
      getSensorForm() {
        sensorRequest.getAllSensorList().then(res => {
          this.sensorForm = res.data
        })
      },
      getModelForm(modelId) {
        modelSensorRequest.getModelSensorById(modelId).then(response => {
          this.modelForm = response.data
        })
      },
      handleSizeChange(pageSize) {
        this.searchModel.pageSize = pageSize
        this.getModelSensorList()
      },
      handleCurrentChange(pageNo) {
        this.searchModel.pageNo = pageNo
        this.getModelSensorList()
      },
      getModelSensorList() {
        // then是调完后的回调方法
        // 在then中把数据传回过来, total 和 list要复制
        modelSensorRequest.getModelSensorList(this.searchModel).then(response => {
          this.totalPage = response.data.totalPage
          this.list = response.data.rows
        })
      },
      handleDialogVisible(id) {
        if (id == null) {
          this.title = '新增项目'
        } else {
          this.title = '修改项目'
          modelSensorRequest.getModelSensorById(id).then(response => {
            this.form = response.data
          })
        }
  
        this.dialogFormVisible = true
      },
      clearForm() {
        this.form = {}
        this.$refs.formRef.clearValidate()
      },
      handleSave() {
        // 触发表单验证
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            modelSensorRequest.saveModelSensor(this.form).then(response => {
              // 成功提示
              this.$message({
                message: response.message,
                type: 'success'
              })
              // 关闭表格
              this.dialogFormVisible = false
              // 刷新表格
              this.getModelSensorList()
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteModelSensor(modelSensor) {
        this.$confirm('此操作将删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          modelSensorRequest.deleteModelSensorById(modelSensor.modelSensorId).then(response => {
            this.$message({
              type: 'success',
              message: response.message
            })
            this.getModelSensorList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      centerHeaderStyle({ row, column, rowIndex, columnIndex }) {
        return { 'text-align': 'center' };
      },
      centerCellStyle({ row, column, rowIndex, columnIndex }) {
          return { 'text-align': 'center' };
      },
    }
  }
  </script>
  
  <style>
    .el-dialog .el-input {
      width: 70%;
    }
  </style>
  